package com.ycc.biz.controller;

import cn.hutool.core.util.StrUtil;
import com.ycc.biz.common.ErrorCode;
import com.ycc.biz.common.Result;
import com.ycc.biz.pojo.dto.UserLoginDto;
import com.ycc.biz.pojo.dto.UserRegisterDto;
import com.ycc.biz.pojo.entity.SysUser;
import com.ycc.biz.exception.BusinessException;
import com.ycc.biz.pojo.vo.LoginUserVO;
import com.ycc.biz.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: SysUserController
 * @author: ycl
 * @date: 2025/03/07 14:18:48
 */
@RestController
@AllArgsConstructor
@Validated
@Api(tags = "系统用户管理")
@RequestMapping("/sysUser")
public class SysUserController {

    private final SysUserService sysUserService;

    @ApiOperation("根据id获取用户")
    @PostMapping("/getById")
    public Result<SysUser> getById(@RequestParam Long id) {
        return Result.success(sysUserService.getById(id));
    }

    @ApiOperation("保存用户")
    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody SysUser user) {
        return Result.success(sysUserService.save(user));
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody SysUser user) {
        return Result.success(sysUserService.updateById(user));
    }

    @ApiOperation("删除用户")
    @PutMapping("/removeById")
    public Result<Boolean> removeById(@RequestParam Long id) {
        return Result.success(sysUserService.removeById(id));
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<String> userRegister(@RequestBody UserRegisterDto userRegisterDto) {
        if (userRegisterDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userRegisterDto.getUserAccount();
        String userPassword = userRegisterDto.getUserPassword();
        String checkPassword = userRegisterDto.getCheckPassword();
        if (StrUtil.isAllBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        String result = sysUserService.userRegister(userAccount, userPassword, checkPassword);
        return Result.success(result);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<LoginUserVO> userLogin(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request) {
        if (userLoginDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginDto.getUserAccount();
        String userPassword = userLoginDto.getUserPassword();
        if (StrUtil.isAllBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LoginUserVO loginUserVO = sysUserService.userLogin(userAccount, userPassword, request);
        return Result.success(loginUserVO);
    }

    @ApiOperation("用户登出")
    @PostMapping("/logout")
    public Result<Boolean> userLogout(HttpServletRequest request) {
        boolean result = sysUserService.userLogout(request);
        return Result.success(result);
    }
}
